package com.lemon.secondhandplatform.ui.activity.user;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.InputType;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

import com.lemon.secondhandplatform.R;
import com.lemon.secondhandplatform.ui.activity.BaseActivity;

import butterknife.BindView;

/**
 * 修改
 */
public class Edit extends BaseActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.editText)
    EditText editText;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit);

        initView();
        bindListener();
    }

    //数据
    public static final String EXTRA_DATA = "Edit.EXTRA_DATA";
    //Toolbar标题
    public static final String EXTRA_TITLE = "Edit.EXTRA_TITLE";

    public static Intent newIntent(Context context, String title, String data) {
        Intent intent = new Intent(context, Edit.class);
        intent.putExtra(EXTRA_TITLE, title);
        intent.putExtra(EXTRA_DATA, data);
        return intent;
    }

    //EditText类型
    public static final String TYPE = "Edit.TYPE";
    //文本域
    public static final String TEXTAREA = "Edit.TEXTAREA";
    //只能输入数字
    public static final String NUMBER = "Edit.NUMBER";
    //输入电话号码
    public static final String PHONE = "Edit.PHONE";

    public static Intent newIntent(Context context, String title, String data, String type) {
        Intent intent = new Intent(context, Edit.class);
        intent.putExtra(EXTRA_TITLE, title);
        intent.putExtra(EXTRA_DATA, data);
        intent.putExtra(TYPE, type);
        return intent;
    }

    private void initView() {
        toolbar.inflateMenu(R.menu.save);

        Intent intent = getIntent();
        String title = intent.getStringExtra(EXTRA_TITLE);
        toolbar.setTitle(title);
        String data = intent.getStringExtra(EXTRA_DATA);
        editText.setText(data);

        String type = intent.getStringExtra(TYPE);
        if (type == null) {
            return;
        }

        if (type.equals(TEXTAREA)) {
            editText.setHeight(200);
        }

        if (type.equals(NUMBER)) {
            editText.setInputType(InputType.TYPE_CLASS_NUMBER);
        }

        if (type.equals(PHONE)) {
            editText.setInputType(InputType.TYPE_CLASS_PHONE);
        }
    }

    /**
     * 配置监听器
     */
    private void bindListener() {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.itemSave:
                        String data = editText.getText().toString();
                        Intent intent = new Intent();
                        intent.putExtra(EXTRA_DATA, data);
                        setResult(RESULT_OK, intent);
                        finish();
                        break;
                }

                return false;
            }
        });
    }
}
